// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt) {
  //author: meizz
  let o = {
    'M+': this.getMonth() + 1, //月份
    'd+': this.getDate(), //日
    'h+': this.getHours(), //小时
    'm+': this.getMinutes(), //分
    's+': this.getSeconds(), //秒
    'q+': Math.floor((this.getMonth() + 3) / 3), //季度
    S: this.getMilliseconds() //毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (let k in o)
    if (new RegExp('(' + k + ')').test(fmt))
      fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  return fmt
}
export const getStartEndOneMonth = date => {
  let dateTemp = null
  if (typeof date === 'object') {
    dateTemp = date
  } else if (typeof date === 'string') {
    dateTemp = new Date(date.replace(/\-/g, '/'))
  } else {
    dateTemp = new Date()
  }
  const year = dateTemp.getFullYear()
  const month = dateTemp.getMonth() + 1
  let sdate = dateTemp.Format('yyyy-MM-01'),
    edate
  if (month == 12) {
    edate = year + 1 + '-01-01'
  } else {
    var m = month + 1
    m = m < 10 ? '0' + m : m
    edate = year + '-' + m + '-01'
  }
  return { sdate, edate }
}

export const getStartEndSixMonth = date => {
  let dateTemp = null
  if (typeof date === 'object') {
    dateTemp = date
  } else if (typeof date === 'string') {
    dateTemp = new Date(date.replace(/\-/g, '/'))
  } else {
    dateTemp = new Date()
  }
  const year = dateTemp.getFullYear()
  const month = dateTemp.getMonth() + 1
  let sdate, edate
  if (month > 5) {
    let m = month - 5
    sdate = year + '-0' + m + '-01'
  } else {
    let m = month - 5 + 12
    m = m < 10 ? '0' + m : m
    sdate = year - 1 + '-' + m + '-01'
  }
  if (month == 12) {
    edate = year + 1 + '-01-01'
  } else {
    let m = month + 1
    m = m < 10 ? '0' + m : m
    edate = year + '-' + m + '-01'
  }
  return { sdate, edate }
}
export const getMonthListBetween = (start, end) => {
  let result = []
  let s = start.split('-')
  let e = end.split('-')
  let min = new Date()
  let max = new Date()
  min.setFullYear(s[0], s[1])
  max.setFullYear(e[0], e[1])
  let curr = min
  while (curr <= max) {
    let month = curr.getMonth()
    let year = curr.getFullYear()
    let m = month < 10 ? '0' + month : month
    if (m == 0) {
      m = 12
      year = year - 1
    }
    result.push(year + '年' + m + '月')
    curr.setMonth(month + 1)
  }
  return result
}
